package com.hmall.cart.listener;

import com.hmall.api.client.CartClient;
import com.hmall.cart.service.impl.CartServiceImpl;
import com.hmall.common.utils.UserContext;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author: zhengsuyang
 * @Desc:
 * @create: 2025-11-06 19:41
 **/
@Component
@RequiredArgsConstructor
public class RemoveCartItems {
    private final CartServiceImpl cartService;
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "cart.clear.queue", durable = "true"),
            exchange = @Exchange(name = "trade.topic",type = ExchangeTypes.TOPIC),
            key = "order.create"))
    public void removeCartItems(Long[] itemIdArray, Message message){
        Long userId = message.getMessageProperties().getHeader("user-info");
        UserContext.setUser(userId);
        System.out.println("用户：" + userId + " 删除购物车商品");
        cartService.removeByItemIds(Arrays.asList(itemIdArray));
        System.out.println("通过MQ删除用户购物车");
    }
/*    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "cart.clear.queue", durable = "true"),
            exchange = @Exchange(name = "trade.topic",type = ExchangeTypes.TOPIC),
            key = "order.create"))
    public void removeCartItems(Long[] itemIdArray){
        Long userId = UserContext.getUser();
        System.out.println("用户：" + userId + " 删除购物车商品");
        cartService.removeByItemIds(Arrays.asList(itemIdArray));
        System.out.println("通过MQ删除用户购物车");
    }*/
}
